//
//  ByCellGetScoreFromPayStore.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/11/8.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellGetScoreFromPayStoreItem.h"

@implementation ByCellGetScoreFromPayStoreItem
{
    UIView          *_bgView;
    
    UIImageView     *_mainView;         // 主图
    UIImageView     *_vipView;          // vip
    UILabel         *_distanceLabel;    // 距离
    UILabel         *_sepLine;          // 分隔线
    UILabel         *_nameLabel;        // 名称
    UILabel         *_scoreLabel;       // 积分
    CAShapeLayer    *_dottedLayer;         // 虚线
    UIButton        *_transferButton;   // 转换
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    _bgView = [UIImageView new];
    _bgView.backgroundColor = [UIColor whiteColor];
    _bgView.layer.borderColor = BY_LINE_COLOR.CGColor;
    _bgView.layer.borderWidth = 1;
    [self.contentView addSubview:_bgView];
    
    _mainView = [UIImageView new];
    _mainView.image = [UIImage imageNamed:@"头视图背景"];
    [_bgView addSubview:_mainView];
    
    _vipView = [UIImageView new];
    _vipView.backgroundColor = BY_RED_COLOR;
    [_bgView addSubview:_vipView];
    
    _distanceLabel = [[UILabel alloc] init];
    _distanceLabel.text = @"200m";
    _distanceLabel.font = FONT(12);
    _distanceLabel.textAlignment = NSTextAlignmentLeft;
    _distanceLabel.textColor = [UIColor lightGrayColor];
    [_bgView addSubview:_distanceLabel];
    
    _sepLine = [UILabel new];
    _sepLine.backgroundColor = BY_LINE_COLOR;
    [_bgView addSubview:_sepLine];

    
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.text = @"天阳电器";
    _nameLabel.font = FONT(14);
    _nameLabel.textAlignment = NSTextAlignmentLeft;
    _nameLabel.textColor = [UIColor blackColor];
    [_bgView addSubview:_nameLabel];
    
    _scoreLabel = [[UILabel alloc] init];
    _scoreLabel.text = @"25000积分";
    _scoreLabel.font = FONT(13);
    _scoreLabel.textAlignment = NSTextAlignmentRight;
    _scoreLabel.textColor = BY_GOLD_COLOR;
    [_bgView addSubview:_scoreLabel];
    
    _dottedLayer = [CAShapeLayer layer];
    _dottedLayer.lineDashPattern = @[@3, @2];
    _dottedLayer.lineWidth = 1;
    _dottedLayer.strokeColor = [UIColor colorWithWhite:0.8 alpha:1].CGColor;
    [_bgView.layer addSublayer:_dottedLayer];
    
    _transferButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _transferButton.titleLabel.font = FONT(14);
    _transferButton.backgroundColor = BY_THEME_COLOR;
    [_transferButton setTitle:@"转换为蜜积分" forState:UIControlStateNormal];
    [_transferButton setTitleColor:BY_GOLD_COLOR forState:UIControlStateNormal];
    [_transferButton addTarget:self action:@selector(transfer) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_transferButton];
    
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(0, 10, 10, 10));
    }];
    // 布局
    [_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(@0);
        make.height.equalTo(_mainView.mas_width).multipliedBy(0.5);
    }];
    [_vipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(_mainView);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    [_distanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@40);
        make.left.equalTo(@10);
        make.top.equalTo(_mainView.mas_bottom);
    }];
    [_sepLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@15);
        make.centerY.equalTo(_distanceLabel);
        make.width.equalTo(@1);
        make.left.equalTo(_distanceLabel.mas_right).offset(10);
    }];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_sepLine.mas_right).offset(10);
        make.top.height.equalTo(_distanceLabel);
    }];
    
    [_scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.equalTo(_nameLabel);
        make.right.equalTo(@-10);
        make.left.greaterThanOrEqualTo(_nameLabel.mas_right).offset(10);
    }];
    [_transferButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(@0);
        make.top.equalTo(_scoreLabel.mas_bottom).offset(-1);
        make.width.equalTo(@120);
    }];
}


- (void)layoutSubviews {
    UIBezierPath *dottedPath = [UIBezierPath bezierPath];
    [dottedPath moveToPoint:CGPointMake(10, self.height - 30 - 10)];
    [dottedPath addLineToPoint:CGPointMake(self.width - 20, self.height - 30 - 10)];
    _dottedLayer.path = dottedPath.CGPath;
}
- (void)setType:(NSInteger)type {
    
    _type = type;
    
    _dottedLayer.hidden = type == 1;
    _transferButton.hidden = type == 1;

    self.backgroundColor = type == 1 ? [UIColor clearColor] : [UIColor whiteColor];
}
- (void)transfer {
    
}
@end
